from datetime import datetime
from django.http.response import HttpResponse
from faker import Faker
from utils.random_time import gen_random_time
from .models import FrontUser, FrontUserExtension

fake = Faker(locale='zh_CN')


def index(request):
    user, _ = FrontUser.objects.get_or_create(u_name='虎虎生威', name='张三')
    print(user)
    return HttpResponse('用户查询成功')


def add_one_to_one_field(request):
    birthday_date = gen_random_time().get('date')
    birthday = datetime.strptime(birthday_date, '%Y-%m-%d %H:%M:%S').date()
    user = FrontUser(u_name=fake.sentence(), name=fake.name())
    user.save()
    user_extension = FrontUserExtension(birthday=birthday, address=fake.address(), user=user)
    user_extension.save()
    return HttpResponse('添加成功')
